package com.magicborrow.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.beans.NormalBean1;
import com.magicborrow.utils.VolleyTool;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class VerticalCodeActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivBack;
    private String phone;
    private String code;
    int i=1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vertical_code);
        findViewById(R.id.btn_ok).setOnClickListener(this);
        findViewById(R.id.btn_send).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                //TODO implement
                phone = getEtPhone().getText().toString();
                if (TextUtils.isEmpty(phone) || phone.length() != 11) {
                    showShortMsg("请输入正确的手机号");
                    return;
                }
                code = getEtCode().getText().toString();
                if (TextUtils.isEmpty(code) || code.length() != 4) {
                    showShortMsg("请输入正确的验证码");
                    return;
                }
                Map<String, String> params1 = new HashMap<>();
                params1.put("mobile", ""+phone);
                params1.put("code", ""+code);
                params1.put("type", "1");
                VolleyTool.get(Constants.CHECK_CODE_URL, params1, new VolleyTool.HTTPListener() {
                    @Override
                    public <T> void onResponse(T t, int requestCode) {
                        String str = t.toString();
                        try {
                            JSONObject obj = new JSONObject(str);
                            int code = obj.getInt("code");
                            String message = obj.getString("message");
                            if (code == 0) {
                                Intent intent = new Intent();
                                intent.putExtra("phone", phone);
                                intent.putExtra("checkCode", obj.getString("data"));
                                intent.setClass(VerticalCodeActivity.this, ForgotPasswordActivity.class);
                                startActivity(intent);
                                finish();
                            } else {
                                Toast.makeText(VerticalCodeActivity.this, message, Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error, int requestCode) {
                        //   dialog.dismiss();
                        //  Toast.makeText(VerticalCodeActivity.this,"服务器忙,请稍微再试",Toast.LENGTH_SHORT).show();
                    }
                }, 0, null);
                break;
            case R.id.btn_send:
                //TODO implement
                phone = getEtPhone().getText().toString();
                if (TextUtils.isEmpty(phone) || phone.length() != 11) {
                    showShortMsg("请输入正确的手机号");
                    return;
                }
                Map<String, String> params = new HashMap<>();
                params.put("mobile", phone);
                params.put("type", "1");
                params.put("channel", ""+i++);
                VolleyTool.get(Constants.GET_CHECK_CODE, params, new VolleyTool.HTTPListener() {
                    @Override
                    public <T> void onResponse(T t, int requestCode) {
                        NormalBean1 bean = (NormalBean1) t;
                        if (bean.getCode() == 0) {
                            Toast.makeText(VerticalCodeActivity.this, "短信已发送到您的手机,请查收", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(VerticalCodeActivity.this, bean.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error, int requestCode) {
                        Toast.makeText(VerticalCodeActivity.this, "服务器忙,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, 1, NormalBean1.class);

                break;
        }
    }

    private EditText getEtPhone() {
        return (EditText) findViewById(R.id.et_phone);
    }

    private EditText getEtCode() {
        return (EditText) findViewById(R.id.et_code);
    }
}
